﻿using EasyTask.EasyService;
using EasyTask.Models;
using EasyTaskUI.UIHelper;
using SQLBuilder.Repositories;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyTaskUI
{
    public partial class FrmMain : Form
    {
        SqliteRepository db = new SqliteRepository(ConnstringConfig.connStr);

        public FrmMain()
        {
            InitializeComponent();

        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            UIHepler.SetDataGridViewStyle(dataGrid);
            RefreshDataGrid();
        }

        private void RefreshDataGrid()
        {
            var source = (from p in db.FindList<Tasks>()
                          select new
                          {
                              p.JobName,
                              p.DisplayJobName,
                              p.Cron,
                              p.DisplayStateDesc,
                              ManualStopIsStart = p.ManualStopIsStart == 0 ? "不执行" : "执行"
                          }).ToList();

            dataGrid.DataSource = source;
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshDataGrid();
        }

        private void dataGrid_DoubleClick(object sender, EventArgs e)
        {
            var row = dataGrid.CurrentRow;
            var jobName=row?.Cells[0]?.Value?.ToString();
            if (!string.IsNullOrEmpty(jobName))
            {
                new FrmEdit
                {
                    JobName = jobName
                }.ShowDialog();
            }
        }
    }
}
